package com.woniuxy;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Stream;


public class Test02 {

    public static void main(String[] args) {
        getAuthors().stream()
                .map(author -> author.getBooks())
                .forEach(books-> System.out.println(books));

        getAuthors().stream()
                .flatMap(author -> author.getBooks().stream())
                .forEach(books-> System.out.println(books));

        getAuthors().stream()
                .map(author -> author.getBooks())
                .flatMap(books->books.stream())
                .forEach(books-> System.out.println(books));
    }

    private static void test10() {
        List<String> strs=new ArrayList<>();
        strs.add("aaa,bbb");
        strs.add("ccc,ddd");
        strs.add("eee,fff");
        strs.stream()
                .map(str -> str.split(","))
                .forEach(str-> System.out.println(Arrays.toString(str)));
        strs.stream()
                .map(str -> str.split(","))
                .flatMap(mystrs->Arrays.stream(mystrs))
                .forEach(str-> System.out.println(str));

        strs.stream()
                .flatMap(str -> Arrays.stream(str.split(",")))
                .forEach(str-> System.out.println(str));
    }

    private static void test09() {
        getAuthors().stream()
                .filter(author -> author.getAge() > 18)
                .map(author->author.getAname())
                .forEach(aname -> System.out.println(aname));
    }

    private static void test08() {
        //作者集合
        List<Author> authors = getAuthors();
        //集合对象转成流式对象：流中的元素还是Author
        authors.stream()
                //筛选:filter(Predicate<? super T> predicate)
                //遍历流中的所有元素，调用test进行判断，会过滤掉返回false的元素
                .filter(author -> author.getAge() > 18)
                //forEach(Consumer<? super T> action);
                //每个元素自动调用void accept(T t)方法
                .forEach(author -> System.out.println(author));
    }

    private static void test07() {
        List<String> strings = new ArrayList<>();
        strings.add("<UNK>");
        strings.add("<UNK>");
        strings.add("<UNK>");
        //Collection接口类型的集合转换成流
        strings.stream();

        Integer[] a={1,2,3,4,5};
        Arrays.stream(a);
        Stream.of(a);
    }


    //Function
    private static void test06() {
        Function<Integer, String> function1=new Function<Integer, String>() {
            @Override
            public String apply(Integer integer) {
                return "Student:"+integer;
            }
        };

        String apply1 = function1.apply(1);
        System.out.println(apply1);

        Function<Integer, String> function2=num-> "Teacher:"+num;
        String apply2 = function2.apply(2);
        System.out.println(apply2);
    }

    //Supplier
    private static void test05() {
        Supplier<Book> supplier1= new Supplier<Book>() {
            @Override
            public Book get() {
                return new Book(1, "book", 1);
            }
        };
        Book book1 = supplier1.get();
        System.out.println(book1);

        Supplier<Book> supplier2=()->new Book(2,"book",1);
        Book book2 = supplier2.get();
        System.out.println(book2);
    }



    //    Consumer
    private static void test04() {
        Consumer<String> consumer1 = new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        };

        consumer1.accept("aaa");

        Consumer<String> consumer2 =s-> {
            String upperCase = s.toUpperCase();
            System.out.println(upperCase);
        };
        consumer2.accept("aaa");

        Consumer<String> consumer3 =s-> System.out.println(s.length());
        consumer3.accept("abcd");
    }



    //Predicate<T>
    private static void test03() {
        //匿名内部类：接口的实现类的对象，（没有定义实现类）
        Predicate<String> predicate1= new Predicate<String>() {
            @Override
            public boolean test(String s) {
                return s.length() >= 3;
            }
        };

        System.out.println(predicate1.test("abc"));
        System.out.println(predicate1.test("ab"));

        //Lambda表达式就是匿名内部类的简写
        //可以推断的就可以省略
        //接口确定了，函数接口，只有一个没实现的方法，要实现方法就确定
        //方法的参数和返回值都确定
        //Lambda的基本语法   (参数)->{方法体}
        //参数类型可以省略
        //如果只有一个参数()可以省
        //如果方法体只有一条语句,return 和;和{}都可以省略
        Predicate<String> predicate2=(String str)->{return str.length()<3;};
        System.out.println(predicate2.test("abc"));

        Predicate<String> predicate3= str->{return str.length()<3;};
        Predicate<String> predicate4= str-> str.length()<3;
    }


    //查询年龄<18岁，出版的数据价格>50的所有图书信息
    private static void test02() {
        List<Author> authors = getAuthors();
        authors.stream()
                .filter(author -> author.getAge() < 18)
                .map(author -> author.getBooks())
                .flatMap(books -> books.stream())
                .filter(book->book.getPrice()>50)
                .forEach(book -> System.out.println(book));
    }

    //查询年龄<18岁，出版的数据价格>50的所有图书信息
    private static void test01() {
        //嵌套地狱
        List<Book> findBooks=new ArrayList<>();
        List<Author> authors = getAuthors();
        for(Author author : authors){
            if(author.getAge()<18){
                List<Book> books = author.getBooks();
                for(Book book : books){
                    if(book.getPrice()>50){
                        findBooks.add(book);
                    }
                }
            }
        }
        System.out.println(findBooks);
    }


    private static List<Author> getAuthors() {
        List<Book> books1 = new ArrayList<>();
        List<Book> books2 = new ArrayList<>();
        List<Book> books3 = new ArrayList<>();
        List<Book> books4 = new ArrayList<>();
        List<Book> books5 = new ArrayList<>();

        books1.add(new Book(1, "书名1", 45));
        books1.add(new Book(2, "书名2", 66));

        books2.add(new Book(3, "书名3", 25));
        books2.add(new Book(4, "书名4", 78));

        books3.add(new Book(5, "书名5", 56));
        books3.add(new Book(6, "书名6", 45));

        books4.add(new Book(7, "书名7", 90));
        books4.add(new Book(8, "书名8", 123));

        books5.add(new Book(9, "书名9", 145));
        books5.add(new Book(10, "书名10", 35));

        Author author1 = new Author(1, "zs", 15, books1);
        Author author2 = new Author(2, "ls", 18, books2);
        Author author3 = new Author(3, "ww", 20, books3);
        Author author4 = new Author(4, "zl", 21, books4);
        Author author5 = new Author(5, "hq", 16, books5);

        return new ArrayList<>(Arrays.asList(author1, author2, author3, author4, author5));
    }
}


